跳到主要内容

状态机播放

播放状态机

你可以使用 playpausestop 方法手动播放和暂停状态机。

useRive(或默认导出的 <Rive /> 组件的 stateMachine prop)上通过 stateMachine 参数指定要播放的状态机名称。

自动播放状态机

要默认自动播放状态机,只需将 autoplay 设置为 true

export default function Simple() {
const { RiveComponent } = useRive({
src: 'https://cdn.rive.app/animations/vehicles.riv',
stateMachine: "bumpy",
autoplay: true,
});

return <RiveComponent />;
}

控制状态机播放

你可以使用 playpausestop 方法手动播放和暂停状态机。

export default function Simple() {
const { rive, RiveComponent } = useRive({
src: "https://cdn.rive.app/animations/vehicles.riv",
stateMachine: "bumpy",
autoplay: true,
});

const handlePlay = useCallback(() => {
rive?.play();
}, [rive]);

const handlePause = useCallback(() => {
rive?.pause();
}, [rive]);

const handleStop = useCallback(() => {
rive?.stop();
}, [rive]);

return (
<div>
<RiveComponent />
<div style={{ marginTop: 12 }}>
<button onClick={handlePlay}>Play</button>
<button onClick={handlePause}>Pause</button>
<button onClick={handleStop}>Stop</button>
</div>
</div>
);
}
看完还有疑问?进群交流下!
与众多 Rive 创作者、开发者一起交流探讨与答疑解惑。
加入交流群